package com.wellness.commons.paging;

import java.io.Serializable;

/**
 * Created by zhuo.shi on 2017/4/27.
 */
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int DEFAULT_PAGE_NO = 1;
    public static final int DEFAULT_PAGE_SIZE = 20;

    private int pageNo;        //当前页码，第x页，默认从第1页开始
    private T items;            //当前页的内容列表

    private int pageSize;       //每页的内容条数，默认每页20条
    private int pageCount;      //总共页数
    private int total;          //总记录条数

    private PageResult(int total, int pageNo, int pageSize, T items) {
        pageNo = pageNo <= 0 ? DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize <= 0 ? DEFAULT_PAGE_SIZE : pageSize;
        this.pageCount = total / pageSize + (total % pageSize == 0 ? 0 : 1);
        this.total = total;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.items = items;
    }

    public static <T> PageResult<T> create(int total, int pageNo, int pageSize, T items) {
        return new PageResult<>(total, pageNo, pageSize, items);
    }

    public int prevPage() {      //上一页
        if (this.pageNo == DEFAULT_PAGE_NO) {
            return this.pageNo;
        }
        return this.pageNo - 1;
    }

    public int nextPage() {     //下一页
        if (this.pageNo == this.pageCount) {
            return this.pageCount;
        }
        return this.pageNo + 1;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public T getItems() {
        return items;
    }

    public void setItems(T items) {
        this.items = items;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public static void main(String[] args) {
        PageResult pr = PageResult.create(107, 1, 20, null);
        System.out.println(pr.getPageCount() + ", " + pr.prevPage() + ", " + pr.nextPage());
    }
}
